package com.qf.purchase.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.purchase.dao.GoodTypeDao;
import com.qf.purchase.po.Brand;
import com.qf.purchase.po.GoodType;
import com.qf.purchase.service.BrandService;
import com.qf.purchase.service.GoodTypeService;
import com.qf.purchase.utils.LayUIOperate;
import com.qf.purchase.utils.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/GoodType")
public class GoodTypeController {
    @Resource
    private GoodTypeService goodTypeService;

    @RequestMapping("/getAllGoodTypeList")
    @ResponseBody
    public TableData getAllGoodTypeList(HttpServletRequest request) {
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String status = request.getParameter("status");

        Map params = new HashMap();
        params.put("name", name);
        if("0".equals(type)){
            params.put("type", null);
        }else {
            params.put("type", type);
        }
        if("0".equals(status)){
            params.put("status", null);
        }else {
            params.put("status", status);
        }

        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<GoodType> data = goodTypeService.getAllGoodTypeByPage(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

    @RequestMapping("/addGoodType")
    @ResponseBody
    public LayUIOperate addGoodType(@RequestBody GoodType goodType) {
        LayUIOperate operate=new LayUIOperate();
        boolean f= goodTypeService.addGoodType(goodType);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("商品类型添加成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("商品类型添加失败");
        }
        return operate;
    }
    @RequestMapping("/updateGoodType")
    @ResponseBody
    public LayUIOperate updateGoodType(@RequestBody GoodType goodType) {
        LayUIOperate operate=new LayUIOperate();
        boolean f= goodTypeService.updateGoodType(goodType);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("商品类型修改成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("商品类型修改失败");
        }
        return operate;
    }
    @RequestMapping("/updateGoodTypeStatus")
    @ResponseBody
    public LayUIOperate updateGoodTypeStatus(HttpServletRequest request) {
        LayUIOperate operate=new LayUIOperate();
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        GoodType goodType = goodTypeService.findGoodTypeById(Integer.parseInt(id));
        goodType.setStatus(status);
        boolean f= goodTypeService.updateGoodType(goodType);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("操作成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("操作失败");
        }
        return operate;
    }

    @RequestMapping("/findGoodTypeById")
    @ResponseBody
    public GoodType findGoodTypeById(HttpServletRequest request) {
        String id = request.getParameter("id");
        GoodType goodType = goodTypeService.findGoodTypeById(Integer.parseInt(id));
        return goodType;
    }
}
